function correlate_eye_data(aux_data,imeta_data,iframe_times,ROI_act,frame_times)
% this function correlates pupil diameter and saccades with movement on the
% ball

[imeta_data]=filter_eye_monitor_data(imeta_data);

if size(aux_data,1)<7
    aux_data(7,:)=0;
end

velM=diff(aux_data(7,:));
velM(velM>5)=velM(velM>5)-10;
velM(velM<-5)=velM(velM<-5)+10;
velM=ftfil(velM,1000,0,10);
velM=[0 velM];

velP=diff(aux_data(2,:));
velP(velP>5)=velP(velP>5)-10;
velP(velP<-5)=velP(velP<-5)+10;
velP=ftfil(velP,1000,0,10);
velP=[0 velP];

pupil_diam=map_ROI_act_to_aux_data(imeta_data(9,:),iframe_times,size(aux_data,2));
pupil_vel=map_ROI_act_to_aux_data(imeta_data(8,:),iframe_times,size(aux_data,2));
ROI_act_aux=map_ROI_act_to_aux_data(ROI_act,frame_times,size(aux_data,2));

if isnan(pupil_diam(1))
    pupil_diam(1)=0;
end

if isnan(pupil_vel(1))
    pupil_vel(1)=0;
end


while sum(isnan(pupil_diam))>0
    nan_ind=isnan(pupil_diam);
    pupil_diam(nan_ind)=pupil_diam(find(nan_ind)-1);
end

while sum(isnan(pupil_vel))>0
    nan_ind=isnan(pupil_vel);
    pupil_vel(nan_ind)=pupil_vel(find(nan_ind)-1);
end

pupil_vel=abs([0 diff(pupil_vel)']);
pupil_vel=double(pupil_vel>3);
xcorr_range=5000;

figure;
subplot(211)
plot([-xcorr_range:xcorr_range],xcorr(abs(velM),pupil_diam,xcorr_range))
title('xcorr vel M - pupil diam.')
axis tight
yl=ylim;
hold on
plot([1 1]*length(pupil_diam),[yl(1) yl(2)],'k')
xlabel('Time [ms]')

subplot(212)
plot(abs(velM)/max(abs(velM)))
hold on
plot(pupil_diam/max(pupil_diam),'r')
axis tight
xlabel('Time [ms]')

figure;
subplot(211)
plot([-xcorr_range:xcorr_range],xcorr(abs(velM),pupil_vel,xcorr_range))
title('xcorr vel M - saccades')
axis tight
yl=ylim;
hold on
plot([1 1]*length(pupil_vel),[yl(1) yl(2)],'k')
xlabel('Time [ms]')

subplot(212)
plot(abs(velM)/max(abs(velM)))
hold on
plot(pupil_vel/max(pupil_vel),'r')
axis tight
xlabel('Time [ms]')

figure;
subplot(211)
plot([-xcorr_range:xcorr_range],xcorr(ROI_act_aux,pupil_diam,xcorr_range))
title('xcorr roi act - pupil diam.')
axis tight
yl=ylim;
hold on
plot([1 1]*length(pupil_vel),[yl(1) yl(2)],'k')
xlabel('Time [ms]')

subplot(212)
plot(abs(ROI_act_aux)/max(abs(ROI_act_aux)))
hold on
plot(pupil_diam/max(pupil_diam),'r')
axis tight
xlabel('Time [ms]')

figure;
subplot(211)
plot([-xcorr_range:xcorr_range],xcorr(ROI_act_aux,pupil_vel,xcorr_range))
title('xcorr roi act - saccades')
axis tight
yl=ylim;
hold on
plot([1 1]*length(pupil_vel),[yl(1) yl(2)],'k')
xlabel('Time [ms]')

subplot(212)
plot(abs(ROI_act_aux)/max(abs(ROI_act_aux)))
hold on
plot(pupil_vel/max(pupil_vel),'r')
axis tight
xlabel('Time [ms]')

